package com.gf.common.bean;

import java.io.Serializable;

/**
 * 分页
 * 
 * @author zhuguifeng
 * 
 */
public class Page implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// 当前页
	private int curpage = 1;
	// 每页的条数
	private int pageSize;
	// 总的条数，由后台查询得到
	private int total;
	// 总的页数
	private int totalPage;

	public int getCurpage() {
		return curpage;
	}

	public void setCurpage(int curpage) {
		this.curpage = curpage;
		this.total = -1;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotal() {
		return total;
	}

	/**
	 * 设置总的条数<br>
	 * 总的数目一旦确定，会对越界的curpage进行修正
	 * 
	 * @param total
	 */
	public void setTotal(int total) {
		this.total = total;
		int pages = total / pageSize;
		boolean hasMore = (0 != total % pageSize);
		if (0 == pages) {
			totalPage = 1;
		} else {
			if (hasMore) {
				totalPage = pages + 1;
			} else {
				totalPage = pages;
			}
		}
		if (curpage > totalPage) {
			curpage = totalPage;
		}
		if (curpage < 1) {
			curpage = 1;
		}
	}

	public boolean isFirst() {
		return (curpage <= 1);
	}

	public boolean isLast() {
		return (curpage >= totalPage);
	}

	public int getTotalPage() {
		return totalPage;
	}

	/**
	 * 返回当前页的第一条 在总的条数的位置<br>
	 * 第一页第一条为0
	 * 
	 * @return
	 */
	public int getStartIndex() {
		return (curpage - 1) * pageSize;
	}

}
